﻿/**
 * 
 * T是泛型参数。where T : class  是对T的限制，这里的意思是T必须是引用类型，包括任何类、接口、委托或数组类型。
 * http://msdn.microsoft.com/zh-cn/library/d5x73970%28v=vs.80%29.aspx 
 * 
 * **/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Reflection;
using System.Configuration;
using LIMS.IDAL;
using LIMS.SqlServerDAL.Properties;
using LIMS.SqlServerDAL;

namespace LIMS.DALFactory
{
    /// <summary>
    ///  工厂创建指定类实例对象
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class DataAccessFactory<T> where T : class
    {

        #region 读配置文件

        /// <summary>
        /// SqlServerDAL程序集名称
        /// </summary>
        private static readonly String dllPath = ConfigurationManager.ConnectionStrings["WebDAL"].ConnectionString;

        /// <summary>
        /// 登陆服务类(LoginService)的命名空间
        /// </summary>
        private static readonly String strLoginService = ConfigurationManager.ConnectionStrings["LoginService"].ConnectionString;

        /// <summary>
        /// 班级服务类（GradeService）的命名空间
        /// </summary>
        private static readonly string strGradeService = ConfigurationManager.AppSettings["GradeService"].ToString();

        /// <summary>
        /// 学生服务类(StudentService)的命名空间
        /// </summary>
        private static readonly String strStudentService = ConfigurationManager.ConnectionStrings["StudentService"].ConnectionString;

        /// <summary>
        /// 留言服务类(MessageService)的命名空间
        /// </summary>
        private static readonly String strMessageService = ConfigurationManager.ConnectionStrings["MessageService"].ConnectionString;

        /// <summary>
        /// 新闻服务类(NewsInfoService)的命名空间
        /// </summary>
        private static readonly String strNewsInfoService = ConfigurationManager.ConnectionStrings["NewsInfoService"].ConnectionString;

        /// <summary>
        /// 实验项目服务类(ProjectService)的命名空间
        /// </summary>
        private static readonly String strProjectService = ConfigurationManager.ConnectionStrings["ProjectService"].ConnectionString;

        /// <summary>
        /// 教师服务类(TeacherService)的命名空间
        /// </summary>
        private static readonly String strTeacherService = ConfigurationManager.ConnectionStrings["TeacherService"].ConnectionString;

        /// <summary>
        /// 实验室预约服务类(OrderlLabService)的命名空间
        /// </summary>
        private static readonly String strOrderlLabService = ConfigurationManager.ConnectionStrings["OrderlLabService"].ConnectionString;

        /// <summary>
        /// 实验室服务类(LabServics)的命名空间
        /// </summary>
        private static readonly String strLabServics = ConfigurationManager.ConnectionStrings["LabServics"].ConnectionString;

        /// <summary>
        /// 学生、实验关联 服务类(StuProjectService)的命名空间
        /// </summary>
        private static readonly String strStuProjectService = ConfigurationManager.ConnectionStrings["StuProjectService"].ConnectionString;

        /// <summary>
        /// 管理员服务类（AdminService）的命名空间 
        /// </summary>
        private static readonly String AdminService = ConfigurationManager.AppSettings["AdminService"].ToString();
        #endregion

        #region 创建缓存工厂

        /// <summary>
        /// 创建缓存工厂
        /// </summary>
        private static DefaultCacheAdapter cacheFactory = new DefaultCacheAdapter();

        #endregion

        #region 创建接口实例

        /// <summary>
        /// 具体创建对象过程
        /// </summary>
        /// <param name="classNameSpace">该实例对象所在的命名空间</param>
        /// <returns>创建好的实例对象</returns>
        private static T CreateType(String classNameSpace)
        {
            //以对象命名空间作缓存key，读缓存值
            Object objData = cacheFactory.Get<T>(classNameSpace);
            if (objData == null)
            {
                //运用反射创建对象实例
                objData = Assembly.Load(dllPath).CreateInstance(classNameSpace);
                //对象的命名空间-->对象实例  key-value  写入缓存
                cacheFactory.Add(classNameSpace, objData);
            }
            return (T)objData;
        }

        /// <summary>
        /// 创建管理员接口的实例对象
        /// </summary>
        /// <returns></returns>
        public static LIMS.IDAL.IAdmin CreateAdmin()
        {
            return (LIMS.IDAL.IAdmin)CreateType(AdminService);
        }

        /// <summary>
        /// 创建班级接口的实例对象
        /// </summary>
        /// <returns></returns>
        public static LIMS.IDAL.IGrade CreateGrade()
        {
            return (LIMS.IDAL.IGrade)CreateType(strGradeService);
        }

        /// <summary>
        /// 创建登陆接口的实例对象
        /// </summary>
        /// <returns></returns>
        public static LIMS.IDAL.ILogin CreateLogin()
        {
            return (LIMS.IDAL.ILogin)CreateType(strLoginService);
        }

        /// <summary>
        /// 创建学生接口的实例对象
        /// </summary>
        /// <returns></returns>
        public static LIMS.IDAL.IStudent CreateStudent()
        {
            return (LIMS.IDAL.IStudent)CreateType(strStudentService);
        }


        /// <summary>
        /// 创建教师接口的实例对象
        /// </summary>
        /// <returns></returns>
        public static LIMS.IDAL.ITeacher CreateTeacher()
        {
            return (LIMS.IDAL.ITeacher)CreateType(strTeacherService);
        }

        /// <summary>
        /// 创建新闻接口的实例对象
        /// </summary>
        /// <returns></returns>
        public static LIMS.IDAL.INewsInfo CreateNewsInfo()
        {
            return (LIMS.IDAL.INewsInfo)CreateType(strNewsInfoService); ;
        }


        /// <summary>
        /// 创建留言接口的实例对象
        /// </summary>
        /// <returns></returns>
        public static LIMS.IDAL.IMessage CreateMessage()
        {
            return (LIMS.IDAL.IMessage)CreateType(strMessageService);
        }

        /// <summary>
        /// 创建实验项目接口的对象实例
        /// </summary>
        /// <returns></returns>
        public static LIMS.IDAL.IProject CreateProject()
        {
            return (LIMS.IDAL.IProject)CreateType(strProjectService);
        }

        /// <summary>
        /// 创建实验室预约的实例对象
        /// </summary>
        /// <returns></returns>
        public static LIMS.IDAL.ILabOrder CreateOrderLab()
        {
            return (LIMS.IDAL.ILabOrder)CreateType(strOrderlLabService);
        }

        /// <summary>
        /// 创建实验室的对象实例
        /// </summary>
        /// <returns></returns>
        public static LIMS.IDAL.ILab CreateLab()
        {
            return (LIMS.IDAL.ILab)CreateType(strLabServics);
        }

        /// <summary>
        /// 创建学生、实验关联类的 实例
        /// </summary>
        /// <returns></returns>
        public static LIMS.IDAL.IStuProject CreateStuProject()
        {
            return (LIMS.IDAL.IStuProject)CreateType(strStuProjectService);
        }

        #endregion



    }

}